import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { HttpService } from '../../../service/httpService';
import { FormBuilder }        from '@angular/forms';
import { NativeService } from '../../../service/NativeService';
@Component({
  selector: 'page-register',
  templateUrl: 'register.html',
})
export class RegisterPage {
  private codeImage:string;
  private regForm:any;
  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private formBuilder: FormBuilder,
              private nat:NativeService,
              private http:HttpService) {
    this.codeImage = http.baseUrl+'/api/AuthCode/CreateImageCode';
    this.regForm = this.formBuilder.group({
      'UserName': ['testasd123'],// 第一个参数是默认值
      'Password': ['123456'],
      'endPassword':['123456'],
      'Phone':['13760423728'],
      'TrueName':['小明'],
      'ExtendCode':[''],
      'AuthCode':[''],
      'IsMobile':['true'],
      'safety':['0']
    });

  }



  ionViewDidLoad() {
  }
  getCodeImage(){
    this.codeImage = this.codeImage+"?"+Math.random();
  }

  registerAction(reg){
    if (reg.UserName.length<=0){
      this.nat.showToast("账号不能为空");
      return;
    }
    if (reg.Password.length <= 0){
      this.nat.showToast("密码不能为空");
      return;
    }
    if (reg.Password != reg.endPassword){
      this.nat.showToast("两次密码不一样");
      return;
    }
    if (reg.Phone.length <= 0){
      this.nat.showToast("手机不能为空");
      return;
    }
    if (reg.TrueName.length <= 0){
      this.nat.showToast("真实姓名不能为空");
      return;
    }
    if (reg.AuthCode.length <= 0){
      this.nat.showToast("验证码不能为空");
      return;
    }
    console.log(reg);
    let url = this.http.baseUrl + "/api/Account/Regist";
    let self = this;
    this.http.post(url,reg,function (res,err) {
      if (!err){
        if (res.StatusCode){
          self.nat.showToast(res.Message);
        }
      }
    })


  }
}
